import schedule
import time
from check_material_tool import *
from Seatalk.sendMessage import *

'''每隔1h查看物料是否缺失直至物料齐全'''


def check_job(C):
    result_list = []

    cur_t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print(f"{cur_t} | 开始检查......")

    html_text = C.get_test_summary()
    C.parse_test_summary(html_text)

    for key in C.issue_dict.keys():
        sit_res = C.check_sit_test_report(key)
        uat_res = C.check_uat_test_report(key)
        prd_res = C.check_PRD_Sign_Off_report(key)
        tech_res = C.check_Tech_design_and_review_report(key)
        sec_res = C.check_security_report(key)
        reg_res = C.check_regression_report(key)
        if not (sit_res and uat_res and prd_res and tech_res and sec_res and reg_res):
        # if not (sit_res and uat_res and prd_res and tech_res and sec_res):
            PRINTD('-' * 80)

    if C.count_error == 0:
        # 物料齐全后停止任务
        print("Stop...")
        schedule.clear()

    C.print_result()
    C.count_error = 0

    if len(L.content) <= 3500:
        sendMessage(L.content, L.mentioned_email_list)
    else:
        sendLongMessage(L.content, L.mentioned_email_list, maximum=3500)
    L.content = ''
    L.mentioned_email_list = []


if __name__ == '__main__':
    C = check_material(username='xunjie.yin@shopee.com',
                       password='yxj@123456',
                       release_jira_url='https://jira.shopee.io/browse/SPDBK-60785',
                       test_summary_url='https://jira.shopee.io/secure/TestSummaryAction.jspa?issueId=3272534')

    # schedule.every(1).hour.at("00:00").do(check_job, C)
    # schedule.every(2).minutes.at(':00').do(check_job, C)
    # while True:
    #     schedule.run_pending()
    #     time.sleep(1)

    check_job(C)

    # C.first_push_material()
